<template>
  <div class="add-address">
    <!-- Header with back button -->
    <div class="header">
      <div class="left" @click="goBack">
        <van-icon name="arrow-left" />
      </div>
      <div class="title">新增收货地址</div>
      <div class="right"></div>
    </div>
    
    <div class="content">
      <!-- 地址选择区域 -->
      <div class="address-select-area">
        <div class="select-box" @click="selectAddress">
          <div class="label">选择收货地址</div>
          <div class="value">
            <span v-if="address.name">{{ address.name }}</span>
            <span v-else class="placeholder">请选择</span>
            <van-icon name="arrow" />
          </div>
        </div>
      </div>
      
      <!-- 门牌号输入 -->
      <div class="detail-input">
        <div class="field-label">门牌号</div>
        <van-field
          v-model="address.detail"
          placeholder="详细地址，例如1栋101室1号"
          type="textarea"
          rows="2"
          clearable
        />
      </div>
      
      <!-- 标签选择 -->
      <div class="tag-selection">
        <div class="field-label">标签</div>
        <div class="tag-options">
          <div 
            v-for="(tag, index) in tagOptions" 
            :key="index"
            class="tag-item"
            :class="{ active: address.tag === tag.value }"
            @click="selectTag(tag.value)"
          >
            <van-icon :name="tag.icon" class="tag-icon" />
            <span>{{ tag.label }}</span>
          </div>
        </div>
      </div>
      
      <!-- 联系人信息 -->
      <div class="contact-info">
        <div class="field-label">联系人</div>
        <van-field
          v-model="address.contact"
          placeholder="请填写收货人姓名"
          clearable
        />
        
        <div class="gender-selection">
          <div 
            class="gender-option" 
            :class="{ active: address.gender === '先生' }"
            @click="address.gender = '先生'"
          >
            <div class="radio-button">
              <div class="inner" v-if="address.gender === '先生'"></div>
            </div>
            <span>先生</span>
          </div>
          
          <div 
            class="gender-option" 
            :class="{ active: address.gender === '女士' }"
            @click="address.gender = '女士'"
          >
            <div class="radio-button">
              <div class="inner" v-if="address.gender === '女士'"></div>
            </div>
            <span>女士</span>
          </div>
        </div>
      </div>
      
      <!-- 手机号 -->
      <div class="phone-input">
        <div class="field-label">手机号</div>
        <div class="phone-field">
          <div class="country-code">+86</div>
          <van-field
            v-model="address.phone"
            placeholder="请填写收货手机号码"
            type="tel"
            clearable
          />
        </div>
      </div>
    </div>
    
    <!-- 底部保存按钮 -->
    <div class="save-button-container">
      <van-button type="primary" block @click="saveAddress">保存地址</van-button>
    </div>
  </div>
</template>

<script>
import { Toast } from 'vant';

export default {
  data() {
    return {
      address: {
        name: '',
        detail: '',
        tag: '',
        contact: '',
        gender: '先生',
        phone: ''
      },
      tagOptions: [
        { label: '家', value: '家', icon: 'wap-home' },
        { label: '公司', value: '公司', icon: 'bag' },
        { label: '学校', value: '学校', icon: 'award' },
        { label: '父母家', value: '父母家', icon: 'friends' },
        { label: '其他', value: '其他', icon: 'location' }
      ]
    };
  },
  created() {
    // 从路由查询参数中获取地址信息
    const { name, address } = this.$route.query;
    if (name) {
      this.address.name = name;
    }
  },
  methods: {
    goBack() {
      this.$router.go(-1);
    },
    selectAddress() {
      // 跳转到地址选择页面
      this.$router.push('/select-location');
    },
    selectTag(tag) {
      this.address.tag = tag;
    },
    saveAddress() {
      // 表单验证
      if (!this.address.name) {
        Toast('请选择收货地址');
        return;
      }
      
      if (!this.address.detail) {
        Toast('请填写详细地址');
        return;
      }
      
      if (!this.address.contact) {
        Toast('请填写联系人');
        return;
      }
      
      if (!this.address.phone) {
        Toast('请填写手机号');
        return;
      }
      
      if (!/^1[3-9]\d{9}$/.test(this.address.phone)) {
        Toast('请填写正确的手机号');
        return;
      }
      
      // 获取已保存的地址
      let savedAddresses = [];
      const savedAddressesStr = localStorage.getItem('savedAddresses');
      if (savedAddressesStr) {
        savedAddresses = JSON.parse(savedAddressesStr);
      }
      
      // 添加新地址
      savedAddresses.unshift(this.address);
      
      // 保存到本地
      localStorage.setItem('savedAddresses', JSON.stringify(savedAddresses));
      
      Toast.success('保存成功');
      
      // 返回上一页
      setTimeout(() => {
        this.$router.go(-1);
      }, 1000);
    }
  }
};
</script>

<style lang="less" scoped>
.add-address {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: #f7f7f7;
  
  .header {
    height: 100px;
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    position: relative;
    
    .left {
      width: 50px;
      height: 50px;
      display: flex;
      align-items: center;
      
      .van-icon {
        font-size: 28px;
        color: #333;
      }
    }
    
    .title {
      font-size: 32px;
      font-weight: 500;
      color: #333;
    }
    
    .right {
      width: 50px;
    }
  }
  
  .content {
    flex: 1;
    overflow-y: auto;
    padding-bottom: 120px;
    
    .address-select-area {
      background-color: #fff;
      margin-top: 20px;
      
      .select-box {
        padding: 30px 20px;
        display: flex;
        flex-direction: column;
        position: relative;
        
        &::after {
          content: "";
          position: absolute;
          left: 20px;
          right: 20px;
          bottom: 0;
          height: 1px;
          background-color: #f0f0f0;
        }
        
        .label {
          font-size: 30px;
          color: #ff9500;
          margin-bottom: 20px;
        }
        
        .value {
          display: flex;
          align-items: center;
          justify-content: space-between;
          font-size: 28px;
          color: #333;
          
          .placeholder {
            color: #999;
          }
          
          .van-icon {
            color: #999;
            font-size: 24px;
          }
        }
      }
    }
    
    .detail-input {
      background-color: #fff;
      padding: 30px 20px;
      
      .field-label {
        font-size: 28px;
        color: #333;
        margin-bottom: 20px;
      }
      
      :deep(.van-field__control) {
        font-size: 28px;
      }
    }
    
    .tag-selection {
      background-color: #fff;
      padding: 30px 20px;
      margin-top: 20px;
      
      .field-label {
        font-size: 28px;
        color: #333;
        margin-bottom: 20px;
      }
      
      .tag-options {
        display: flex;
        flex-wrap: wrap;
        
        .tag-item {
          width: 20%;
          display: flex;
          flex-direction: column;
          align-items: center;
          justify-content: center;
          margin-bottom: 20px;
          
          .tag-icon {
            font-size: 40px;
            margin-bottom: 10px;
            color: #666;
          }
          
          span {
            font-size: 26px;
            color: #333;
          }
          
          &.active {
            .tag-icon, span {
              color: #ff9500;
            }
          }
        }
      }
    }
    
    .contact-info {
      background-color: #fff;
      padding: 30px 20px;
      margin-top: 20px;
      
      .field-label {
        font-size: 28px;
        color: #333;
        margin-bottom: 20px;
      }
      
      :deep(.van-field__control) {
        font-size: 28px;
      }
      
      .gender-selection {
        display: flex;
        margin-top: 20px;
        
        .gender-option {
          display: flex;
          align-items: center;
          margin-right: 60px;
          
          .radio-button {
            width: 36px;
            height: 36px;
            border-radius: 50%;
            border: 1px solid #ddd;
            margin-right: 10px;
            display: flex;
            align-items: center;
            justify-content: center;
            
            .inner {
              width: 24px;
              height: 24px;
              border-radius: 50%;
              background-color: #ffe74c;
            }
          }
          
          span {
            font-size: 28px;
            color: #333;
          }
          
          &.active {
            .radio-button {
              border-color: #ffe74c;
            }
          }
        }
      }
    }
    
    .phone-input {
      background-color: #fff;
      padding: 30px 20px;
      margin-top: 20px;
      
      .field-label {
        font-size: 28px;
        color: #333;
        margin-bottom: 20px;
      }
      
      .phone-field {
        display: flex;
        align-items: center;
        
        .country-code {
          font-size: 28px;
          color: #333;
          margin-right: 15px;
          flex-shrink: 0;
        }
        
        :deep(.van-field) {
          flex: 1;
        }
        
        :deep(.van-field__control) {
          font-size: 28px;
        }
      }
    }
  }
  
  .save-button-container {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    padding: 20px;
    background-color: #fff;
    box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.05);
    box-sizing: border-box;
    
    :deep(.van-button) {
      height: 80px;
      line-height: 80px;
      font-size: 30px;
      border-radius: 40px;
      background-color: #ffe74c;
      border-color: #ffe74c;
      color: #333;
    }
  }
}
</style> 